package com.atguigu.yygh.order.controller;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.order.utils.AuthContextHolder;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 */
@Api(tags = "订单系统")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {


    @Autowired
    private OrderInfoService orderInfoService;

    @PostMapping("/submitOrder/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable String scheduleId,
                         @PathVariable Long patientId){

        Long orderId=orderInfoService.submitOrder(scheduleId,patientId);
       return R.ok().data("orderId",orderId);
    }


    //
    @ApiOperation(value = "查询当前用户的历史订单")
    @GetMapping("orders/{pageNum}/{pageSize}")
    public R getOrdersByQueryCondition(@PathVariable Integer pageNum,
                                       @PathVariable Integer pageSize,
                                       OrderQueryVo orderQueryVo,
                                       HttpServletRequest request){

        Long userId = AuthContextHolder.getUserId(request);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> page= orderInfoService.getOrdersByQueryCondition(pageNum,pageSize,orderQueryVo);
        return R.ok().data("page",page);
    }

    //获取订单状态列表
    @GetMapping("/status/list")
    public R getOrderStatusList(){
        return R.ok().data("list",OrderStatusEnum.getStatusList());
    }

    @GetMapping("/detail/{orderId}")
    public R getOrderDetail(@PathVariable Long orderId ){
        OrderInfo orderInfo=orderInfoService.getOrderDetail(orderId);
        return R.ok().data("orderInfo",orderInfo);
    }

    @GetMapping(value="auth/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable Long orderId){
        orderInfoService.cancelOrder(orderId);
        return R.ok();
    }




    @PostMapping("/countOrderInfo")
    public Map<String,Object> countOrderInfo(@RequestBody OrderCountQueryVo orderQueryVo){
        return orderInfoService.countOrderInfo(orderQueryVo);
    }

}

